package com.D5Power.Editor
{
	import com.D5Power.loader.MutiLoader;
	
	import flash.display.Loader;
	import flash.events.Event;
	import flash.filesystem.File;
	import flash.net.URLLoader;
	import flash.system.ApplicationDomain;
	
	import org.aswing.JList;
	import org.aswing.JPanel;
	import org.aswing.JScrollPane;
	import org.aswing.LayoutManager;
	import org.aswing.VectorListModel;
	import org.aswing.event.ListItemEvent;

	/**
	 * 资料库显示
	 */ 
	public class LibResource extends JPanel
	{
		
		private var lib:ApplicationDomain;

		private var lib_box:String;
		
		private var module:VectorListModel;
		
		private var list:JList;
		
		private var padding:uint = 5;
		
		private var _root:MapEditor;
		
		/**
		 * 资源列表
		 */ 
		private var fileList:Array;
		
		public static const MAP:String = 'Map';
		
		public static const CHARACTER:String = 'Character';
		
		/**
		 * @param	layout	布局
		 * @param	editor	MapEditor对象
		 * @param	libname	库资源列表名
		 */ 
		public function LibResource(box:String,layout:LayoutManager=null,editor:MapEditor=null)
		{
			super(layout);
			_root = editor;
			lib_box = box;
		}
		
		/**
		 * 分析完成后生成可用资源列表
		 * @param	data	获取的资源列表
		 */ 
		public function build(id:uint):void
		{
			
			var fp:File = lib_box==MAP ? File.applicationDirectory.resolvePath('asset/map/map'+id) : File.applicationDirectory.resolvePath('asset/map/map'+id+'/npc');
			if(!fp.exists)
			{
				_root.msg('提示',"资源目录不存在！请检查！");
				return;
			}
			
			// 产生资源列表
			if(fileList!=null) fileList.splice(0,fileList.length);
			fileList = fp.getDirectoryListing();
			
			module = new VectorListModel();
			for each(var f:File in fileList)
			{
				module.append(f.name);
			}
			
			list = new JList(module);
			list.setVisibleCellWidth(D5Power.SMALLMAPSIZE);
			list.setVisibleRowCount(10);
			list.addEventListener(ListItemEvent.ITEM_CLICK,onClick);
			append(new JScrollPane(list));
		}
		
		/**
		 * 当点击某一资源名时，触发编辑器的增加游戏对象方法
		 */ 
		private function onClick(e:ListItemEvent):void
		{
			var source:String = e.getCell().getCellValue();
			var temp:String;
			switch(lib_box)
			{
				case MAP:
					temp = source.split('.')[0];
					_root.Shower.startAddObject(source,lib_box,temp);
					break;
				case CHARACTER:
					temp = source.split('.')[0];
					_root.Shower.startAddObject(source,lib_box,temp);
					break;
			}
			
		}
	}
}